package com.ccyy.manage.controller;


import com.ccyy.manage.model.BaseResponse;
import com.ccyy.manage.model.entity.Dept;
import com.ccyy.manage.service.DeptService;
import com.ccyy.manage.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 是超超越越啊~
 * @since 2021-07-20
 */
@RestController
@RequestMapping("/dept")
@RequiresAuthentication
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private JwtUtil jwtUtil;


    @ApiOperation("根据当前用户获取部门列表")
    @GetMapping("/list")
    @ResponseBody
    public BaseResponse getList(HttpServletRequest request) {
//       获取用户名
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        String username = jwtUtil.getClaimByToken(token).getSubject();
        List<Dept> depts = deptService.selectAllDeptTree(username);
        return BaseResponse.success(depts);
    }

    @ApiOperation("新增部门")
    @PostMapping
    @ResponseBody
    public BaseResponse add(Dept dept) {
        return BaseResponse.success(deptService.save(dept));
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{deptId}")
    @ResponseBody
    public BaseResponse delete(@PathVariable("deptId") Long deptId) {
        deptService.deleteCascadeByID(deptId);
        return BaseResponse.success();
    }

    @ApiOperation("修改部门")
    @PutMapping
    @ResponseBody
    public BaseResponse update(Dept dept) {
        deptService.updateById(dept);
        return BaseResponse.success();
    }

    @ApiOperation("获取单个部门信息")
    @GetMapping("/{deptId}")
    public BaseResponse update(@PathVariable("deptId") Long deptId) {
        Dept dept = deptService.getById(deptId);
        return BaseResponse.success(dept);
    }


}
